package com.example.memorypoint.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.R;
import com.example.memorypoint.entity.Diary;
import com.example.memorypoint.mvp.history.HistoryContract;
import com.example.memorypoint.mvp.history.HistoryPresenterImpl;
import com.example.memorypoint.utils.DotTimeLineAdapter;
import com.example.memorypoint.utils.Event;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity implements HistoryContract.HistoryView {

    private RecyclerView mRecyclerView;
    private Toolbar toolbar;
    private DotTimeLineAdapter mAdapter;
    private List<Event> mList = new ArrayList<>();
    private DiaryDatabase database;
    private HistoryContract.HistoryPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        // 初始化控件
        mRecyclerView = findViewById(R.id.recycler_view);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        // 初始化数据库
        database = new DiaryDatabase(getBaseContext());
        // 初始化 RecyclerView
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new DotTimeLineAdapter(this, mList);
        mRecyclerView.setAdapter(mAdapter);

        // 创建 Presenter，并加载数据
        presenter = new HistoryPresenterImpl(this, database);
        presenter.loadHistory();
    }

    /**
     * HistoryContract.HistoryView 接口方法
     * 将获取到的 Diary 数据转换为 Event 数据，更新列表适配器。
     */
    @Override
    public void showHistory(List<Diary> diaries) {
        mList.clear();
        // 根据你的原始逻辑，这里将 diary.getDate() 和 diary.getTitle() 分别作为时间和事件描述
        for (Diary diary : diaries) {
            Event event = new Event();
            event.setTime(diary.getDate());
            event.setEvent(diary.getTitle());
            mList.add(event);
        }
        mAdapter.setList(mList);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * HistoryContract.HistoryView 接口方法：显示错误信息
     */
    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    // 处理 Toolbar 返回按钮点击
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // 在 Activity 销毁时分离 Presenter 以避免内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
}
